package com.lt.shiropro.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lt.shiropro.common.CommonConstant;
import com.lt.shiropro.entity.User;
import com.lt.shiropro.entity.dto.LoginDto;
import com.lt.shiropro.result.Result;
import com.lt.shiropro.service.UserService;
import com.lt.shiropro.util.JwtUtil;
import com.lt.shiropro.util.Md5Utils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 用户模块
 * @version 1.0
 * @Author molan
 * @Date 2023/12/2 22:37
 * @注释
 */
@RestController
@RequestMapping("/user")
//@Api(tags="用户模块")
public class UserController {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    private UserService userService;

    /**
     * 登录接口
     * @param loginDto 登陆实体类包含用户名和密码
     * @return
     */
//    @ApiOperation(value="登录", notes="登录")
    @PostMapping("/login")
    public Result<Object> login(@RequestBody LoginDto loginDto){
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        //用户名和密码不能为空
        if(!StringUtils.hasText(username) || !StringUtils.hasText(password)){
            return Result.error("用户名或密码不能为空");
        }
        //通过用户名查询用户
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if(Objects.isNull(user)){
            return Result.error("查询不到该用户");
        }
        //匹配密码
        String rePassword = Md5Utils.md5(password);
        if(!rePassword.equals(user.getPassword())){
            return Result.error("密码输入错误");
        }

        //生成token返回给前端，并保存到redis
        String token = JwtUtil.sign(username,password);
        //设置过期时间为24小时
        stringRedisTemplate.opsForValue().set(CommonConstant.PREFIX_USER_TOKEN + token,token,JwtUtil.EXPIRE_TIME*2, TimeUnit.SECONDS);
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        //3.设置登录用户信息
        map.put("userInfo", user);
        return Result.OK(map);
    }

    /**
     * 注册接口
     * @param loginDto 注册实体类包含用户名和密码
     * @return
     */
//    @ApiOperation(value="注册", notes="注册")
    @PostMapping("/register")
    public Result<Object> register(@RequestBody LoginDto loginDto){
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        //用户名和密码不能为空
        if(!StringUtils.hasText(username) || !StringUtils.hasText(password)){
            return Result.error("用户名或密码不能为空");
        }
        //通过用户名查询用户
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if(Objects.nonNull(user)){
            return Result.error("已存在该用户名");
        }
        //生成加密后的密码
        String rePassword = Md5Utils.md5(password);
        User registerUser = new User();
        registerUser.setUsername(username);
        registerUser.setPassword(rePassword);
        registerUser.setStatus(1);
        boolean save = false;
        try {
           save=  userService.save(registerUser);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }

        if(save){
            return Result.oK("注册成功");
        }else {
            return Result.error("注册失败");
        }
    }

    @GetMapping("/getRegister")
    public Result<String> getRegister(){
        return Result.oK();
    }

    @GetMapping("/getRegisterRoles")
    @RequiresRoles("admin")
    public Result<String> getRegisterRoles(){
        return Result.oK();
    }

    @GetMapping("/getRegisterPers")
    @RequiresPermissions("admin:select")
    public Result<String> getRegisterPers(){
        return Result.oK();
    }

}
